package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.UserRepository;
import org.geektimes.projects.user.web.repository.DerbyUserRepository;
import org.geektimes.projects.user.web.service.UserService;
import org.geektimes.projects.user.web.service.impl.UserServiceImpl;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("/register")
public class RegisterController implements PageController {

    private UserService userService;

//    public UserService getUserService() {
//        return userService;
//    }
//
//    public void setUserService(UserServiceImpl userServiceImpl) {
//        this.userService = userServiceImpl;
//    }

    @GET
    @POST
    @Path("/submit")
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {

        User user = new User();

        String name = request.getParameter("name");
        //servletContext.log("name"+name);
        System.out.println("name"+name);
        user.setName(name);
        String email = request.getParameter("email");
        //servletContext.log("email"+email);
        System.out.println("email"+email);
        user.setEmail(email);
        String phone = request.getParameter("phoneNumber");
        //servletContext.log("phone"+phone);
        System.out.println("phone"+phone);
        user.setPhoneNumber(phone);
        String[] passwordArray = request.getParameterValues("password");
        //servletContext.log("password"+passwordArray[0]);
        //servletContext.log("repassword"+passwordArray[1]);
        System.out.println("password"+passwordArray[0]);
        System.out.println("repassword"+passwordArray[1]);

        user.setPassword(passwordArray[0]);


        userService = new UserServiceImpl();
        userService.registerUser(user);

        return "success.jsp";
    }
}
